package gw.com.android.utils;

import android.content.res.TypedArray;
import android.graphics.drawable.Drawable;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import com.gwtsz.chart.output.utils.ChartThemeUtil;
import com.gwtsz.gts2.hx.R;

import gw.com.android.app.AppContances;
import gw.com.android.app.GTConfig;
import www.com.library.app.AppActivities;

/**
 * 颜色类
 **/
public class ColorThemeUtil {

    private static final int[] ATTRS = new int[]{
            R.attr.theme_color_a,
            R.attr.theme_color_b,
            R.attr.theme_color_c,
            R.attr.theme_color_d,
            R.attr.theme_color_e,
            R.attr.theme_color_f,
            R.attr.theme_color_g,
            R.attr.theme_color_h,
            R.attr.theme_color_i,
            R.attr.theme_color_j,
            R.attr.theme_color_k,
            R.attr.theme_color_l,
            R.attr.theme_color_m,
            R.attr.theme_color_n,
            R.attr.theme_color_o,
            R.attr.theme_color_p,
            R.attr.theme_color_q,
            R.attr.theme_color_r,
            R.attr.theme_color_s,
            R.attr.theme_color_t,
            R.attr.theme_color_chart_a,
            R.attr.theme_color_chart_b,
            R.attr.theme_color_chart_c,
            R.attr.theme_color_chart_k,
            R.attr.theme_color_chart_ap,
            R.attr.theme_color_chart_bp,
            R.attr.theme_color_chart_cp,
            R.attr.theme_color_gdpage_lip,
            R.attr.theme_color_gdpage_lip_p,
            R.attr.theme_transparent,
            R.attr.theme_dialog_btn_bg_selector,
            R.attr.theme_quote_edit_item_bg,
            R.attr.theme_quote_edit_select_bg,
            R.attr.theme_quote_edit_add_bg,
            R.attr.theme_color_trade_buy,
            R.attr.theme_color_trade_sell,
            R.attr.theme_color_code_a,
            R.attr.theme_color_code_b,
            R.attr.theme_color_code_c,
            R.attr.theme_color_iconbg
    };

    public int RiseColor;//上涨颜色值
    public int FallColor;//下跌颜色值
    public int IdleColor;// 持平颜色值
    public int color_red;// 错误提示颜色值
    public int color_a;
    public int color_b;
    public int color_c;
    public int color_d;
    public int color_e;
    public int color_f;
    public int color_g;
    public int color_h;
    public int color_i;
    public int color_j;
    public int color_k;
    public int color_l;
    public int color_m;
    public int color_n;
    public int color_o;
    public int color_p;
    public int color_q;
    public int color_r;
    public int color_s;
    public int color_t;
    public int color_chart_k;
    public int color_chart_ap;
    public int color_chart_bp;
    public int color_chart_cp;
    public int color_gdpage_lip;
    public int color_gdpage_lip_p;
    public int transparent;
    public int color_trade_buy;
    public int color_trade_sell;
    public int color_code_a;
    public int color_code_b;
    public int color_code_c;
    public int color_iconbg;
    public Drawable dialog_btn_bg_selector;
    public Drawable quote_edit_item_bg;
    public Drawable quote_edit_select_bg;
    public Drawable quote_edit_add_bg;

    public boolean isGreenRise = true;
    private static ColorThemeUtil mInstance;

    public static ColorThemeUtil instance() {
        if (null == mInstance) {
            mInstance = new ColorThemeUtil();
            if (GTConfig.instance().getPrefColorConfig().equals(GTConfig.COLOR_GREEN_RISE)) {
                mInstance.DefaultColorTheme();
            } else {
                mInstance.changeColorTheme();
            }
        }

        return mInstance;
    }

    public void DefaultColorTheme() {
        ChartThemeUtil.instance().setColorConfig(true);
        final TypedArray a = AppActivities.getSingleton().currentActivity().obtainStyledAttributes(ATTRS);
        color_a = a.getColor(0, 0);
        color_b = a.getColor(1, 0);
        color_c = a.getColor(2, 0);
        color_d = a.getColor(3, 0);
        color_e = a.getColor(4, 0);
        color_f = a.getColor(5, 0);
        color_g = a.getColor(6, 0);
        color_h = a.getColor(7, 0);
        color_i = a.getColor(8, 0);
        color_j = a.getColor(9, 0);
        color_k = a.getColor(10, 0);
        color_l = a.getColor(11, 0);
        color_m = a.getColor(12, 0);
        color_n = a.getColor(13, 0);
        color_o = a.getColor(14, 0);
        color_p = a.getColor(15, 0);
        color_q = a.getColor(16, 0);
        color_r = a.getColor(17, 0);
        color_s = a.getColor(18, 0);
        color_t = a.getColor(19, 0);
        RiseColor = a.getColor(20, 0);
        FallColor = a.getColor(21, 0);
        IdleColor = a.getColor(22, 0);
        color_chart_k = a.getColor(23, 0);
        color_chart_ap = a.getColor(24, 0);
        color_chart_bp = a.getColor(25, 0);
        color_chart_cp = a.getColor(26, 0);
        color_gdpage_lip = a.getColor(27, 0);
        color_gdpage_lip_p = a.getColor(28, 0);
        transparent = a.getColor(29, 0);
        dialog_btn_bg_selector = a.getDrawable(30);
        quote_edit_item_bg = a.getDrawable(31);
        quote_edit_select_bg = a.getDrawable(32);
        quote_edit_add_bg = a.getDrawable(33);
        color_trade_buy = a.getColor(34, 0);
        color_trade_sell = a.getColor(35, 0);
        color_code_a = a.getColor(36, 0);
        color_code_b = a.getColor(37, 0);
        color_code_c = a.getColor(38, 0);
        color_iconbg = a.getColor(39, 0);
        color_red = a.getColor(21, 0);
        isGreenRise = true;
        a.recycle();
    }

    public void changeColorTheme() {
        ChartThemeUtil.instance().setColorConfig(false);
        final TypedArray a = AppActivities.getSingleton().currentActivity().obtainStyledAttributes(ATTRS);
        color_a = a.getColor(0, 0);
        color_b = a.getColor(1, 0);
        color_c = a.getColor(2, 0);
        color_d = a.getColor(3, 0);
        color_e = a.getColor(4, 0);
        color_f = a.getColor(5, 0);
        color_g = a.getColor(6, 0);
        color_h = a.getColor(7, 0);
        color_i = a.getColor(8, 0);
        color_j = a.getColor(9, 0);
        color_k = a.getColor(10, 0);
        color_l = a.getColor(11, 0);
        color_m = a.getColor(12, 0);
        color_n = a.getColor(13, 0);
        color_o = a.getColor(14, 0);
        color_p = a.getColor(15, 0);
        color_q = a.getColor(16, 0);
        color_r = a.getColor(17, 0);
        color_s = a.getColor(18, 0);
        color_t = a.getColor(19, 0);
        FallColor = a.getColor(20, 0);
        RiseColor = a.getColor(21, 0);
        IdleColor = a.getColor(22, 0);
        color_chart_k = a.getColor(23, 0);
        color_chart_ap = a.getColor(24, 0);
        color_chart_bp = a.getColor(25, 0);
        color_chart_cp = a.getColor(26, 0);
        color_gdpage_lip = a.getColor(27, 0);
        color_gdpage_lip_p = a.getColor(28, 0);
        transparent = a.getColor(29, 0);
        dialog_btn_bg_selector = a.getDrawable(30);
        quote_edit_item_bg = a.getDrawable(31);
        quote_edit_select_bg = a.getDrawable(32);
        quote_edit_add_bg = a.getDrawable(33);
        color_trade_buy = a.getColor(34, 0);
        color_trade_sell = a.getColor(35, 0);
        color_code_a = a.getColor(36, 0);
        color_code_b = a.getColor(37, 0);
        color_code_c = a.getColor(38, 0);
        color_iconbg = a.getColor(39, 0);
        color_red = a.getColor(21, 0);
        isGreenRise = false;
        a.recycle();
    }

    /**
     * 根据涨跌情况返回颜色值
     */
    public int getPriceColor(int direction) {
        int color = IdleColor;
        switch (direction) {
            case AppContances.PRICE_INCREASE:
                color = RiseColor;
                break;
            case AppContances.PRICE_DECREASE:
                color = FallColor;
                break;
            case AppContances.PRICE_UNCHANGE:
                color = IdleColor;
                break;
        }
        return color;
    }

    /**
     * 根据涨跌情况返回颜色值
     */
    public int getPriceBg(int direction) {
        int color = R.drawable.price_unchange_bg;
        switch (direction) {
            case AppContances.PRICE_INCREASE:
                if (isGreenRise) {
                    color = R.drawable.price_rise_bg;
                } else {
                    color = R.drawable.price_drop_bg;
                }
                break;
            case AppContances.PRICE_DECREASE:
                if (isGreenRise) {
                    color = R.drawable.price_drop_bg;
                } else {
                    color = R.drawable.price_rise_bg;
                }
                break;
            case AppContances.PRICE_UNCHANGE:
                color = R.drawable.price_unchange_bg;
                break;
        }

        return color;
    }

    /**
     * 根据涨跌情况返回颜色值
     */
    public int getNewPriceBg(int direction, boolean isNight) {
        int color = R.color.color_chart_c;
        switch (direction) {
            case AppContances.PRICE_INCREASE:
                if (isGreenRise) {
                    color = isNight ? R.color.color_quote_g : R.color.color_chart_aa;
                } else {
                    color = isNight ? R.color.color_quote_f : R.color.color_chart_bb;
                }
                break;
            case AppContances.PRICE_DECREASE:
                if (isGreenRise) {
                    color = isNight ? R.color.color_quote_f : R.color.color_chart_bb;
                } else {
                    color = isNight ? R.color.color_quote_g : R.color.color_chart_aa;
                }
                break;
            case AppContances.PRICE_UNCHANGE:
                color = R.color.color_chart_cc;
                break;
        }

        return color;
    }

    /**
     * 根据涨跌情况返回drawable
     */
    public int getNewPriceBgDrawable(int direction, boolean isNight) {//暂时不考虑黑夜模式，因为UI没有给出黑夜模式的设计稿
        int drawable = R.drawable.shape_quote_default;
        switch (direction) {
            case AppContances.PRICE_INCREASE:
                if (isGreenRise) {
                    drawable = R.drawable.shape_quote_green;
                } else {
                    drawable = R.drawable.shape_quote_red;
                }
                break;
            case AppContances.PRICE_DECREASE:
                if (isGreenRise) {
                    drawable = R.drawable.shape_quote_red;
                } else {
                    drawable = R.drawable.shape_quote_green;
                }
                break;
            case AppContances.PRICE_UNCHANGE:
                drawable = R.drawable.shape_quote_default;
                break;
        }

        return drawable;
    }

    public int getPriceArrow(int direction) {
        int resId = 0;
        switch (direction) {
            case AppContances.PRICE_INCREASE:
                if (isGreenRise) {
                    resId = R.mipmap.a_pop_green_arrow_up;
                } else {
                    resId = R.mipmap.a_pop_red_arrow_up;
                }
                break;
            case AppContances.PRICE_DECREASE:
                if (isGreenRise) {
                    resId = R.mipmap.a_pop_red_arrow_down;
                } else {
                    resId = R.mipmap.a_pop_green_arrow_down;
                }
                break;
            case AppContances.PRICE_UNCHANGE:
                break;
            default:
                break;
        }
        return resId;
    }

    public int getQuotePriceColor(int direction) {
        int color = R.color.color_chart_c;
        switch (direction) {
            case AppContances.PRICE_INCREASE:
                if (isGreenRise) {
                    color = R.color.color_chart_a;
                } else {
                    color = R.color.color_chart_b;
                }
                break;
            case AppContances.PRICE_DECREASE:
                if (isGreenRise) {
                    color = R.color.color_chart_b;
                } else {
                    color = R.color.color_chart_a;
                }
                break;
            case AppContances.PRICE_UNCHANGE:
                color = R.color.color_chart_c;
                break;
        }

        return color;
    }


    /**
     * 根据涨跌情况显示颜色值和控件
     */
    public void setPriceColor(TextView priceView, int direction) {
        priceView.setTextColor(getPriceColor(direction));
    }

    /**
     * 根据涨跌情况显示颜色值和控件
     *
     * @param isRight 箭头方向是否放右边
     */
    public void setPriceColor(TextView priceView, int direction, int arrawState, boolean isRight) {
        int arrawId = R.mipmap.a_price_standand;
        int color = IdleColor;
        switch (direction) {
            case AppContances.PRICE_DECREASE:
                color = FallColor;
                if (arrawState == AppContances.ARRAW_HIDE) {
                    arrawId = R.mipmap.a_price_standand;
                } else {
                    if (isGreenRise) {
                        arrawId = R.mipmap.a_price_down;
                    } else {
                        arrawId = R.mipmap.a_price_opp_down;
                    }
                }
                break;
            case AppContances.PRICE_INCREASE:
                color = RiseColor;
                if (arrawState == AppContances.ARRAW_HIDE) {
                    arrawId = R.mipmap.a_price_standand;
                } else {
                    if (isGreenRise) {
                        arrawId = R.mipmap.a_price_up;
                    } else {
                        arrawId = R.mipmap.a_price_opp_up;
                    }
                }
                break;
            case AppContances.PRICE_UNCHANGE:
                color = IdleColor;
                arrawId = R.mipmap.a_price_standand;
                break;
        }
        priceView.setTextColor(color);
        if (isRight) {
            priceView.setCompoundDrawablesWithIntrinsicBounds(0, 0, arrawId, 0);
        } else {
            priceView.setCompoundDrawablesWithIntrinsicBounds(arrawId, 0, 0, 0);
        }
    }

    /**
     * 根据涨跌情况显示颜色值和控件(图表页面现价显示）
     *
     * @param priceView
     * @param direction
     * @param arrawState 1标识不需要箭头，0标识要显示箭头
     * @param isRight    箭头方向是否放右边
     */
    public void setNewPriceColor(TextView priceView, int direction, int arrawState, boolean isRight) {
        int arrawId = 0;
        int color = IdleColor;
        switch (direction) {
            case AppContances.PRICE_DECREASE:
                color = FallColor;
                if (arrawState != AppContances.ARRAW_HIDE) {
                    if (isGreenRise) {
                        arrawId = R.mipmap.a_bigarrow_down;
                    } else {
                        arrawId = R.mipmap.a_bigarrow_switch_down;
                    }
                }
                break;
            case AppContances.PRICE_INCREASE:
                color = RiseColor;
                if (arrawState != AppContances.ARRAW_HIDE) {
                    if (isGreenRise) {
                        arrawId = R.mipmap.a_bigarrow_up;
                    } else {
                        arrawId = R.mipmap.a_bigarrow_switch_up;
                    }
                }
                break;
            case AppContances.PRICE_UNCHANGE:
                color = IdleColor;
                break;
        }
        priceView.setTextColor(color);
        if (isRight) {
            priceView.setCompoundDrawablesWithIntrinsicBounds(0, 0, arrawId, 0);
        } else {
            priceView.setCompoundDrawablesWithIntrinsicBounds(arrawId, 0, 0, 0);
        }
    }

    public void setNewPriceColor(TextView priceView, ImageView arrow, int direction, int arrawState) {
        int arrawId = 0;
        int color = IdleColor;
        switch (direction) {
            case AppContances.PRICE_DECREASE:
                color = FallColor;
                if (arrawState != AppContances.ARRAW_HIDE) {
                    if (isGreenRise) {
                        arrawId = R.mipmap.a_red_down;
                    } else {
                        arrawId = R.mipmap.a_green_down;
                    }
                }
                break;
            case AppContances.PRICE_INCREASE:
                color = RiseColor;
                if (arrawState != AppContances.ARRAW_HIDE) {
                    if (isGreenRise) {
                        arrawId = R.mipmap.a_green_up;
                    } else {
                        arrawId = R.mipmap.a_red_up;
                    }
                }
                break;
            case AppContances.PRICE_UNCHANGE:
                color = IdleColor;
                break;
        }
        priceView.setTextColor(color);
        if (arrawId == 0) {
            arrow.setVisibility(View.GONE);
        } else {
            arrow.setVisibility(View.VISIBLE);
            arrow.setImageResource(arrawId);
        }
    }

}
